package br.edu.unifei.mestrado.db.graph;

import java.util.Iterator;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.index.Index;

import br.edu.unifei.mestrado.commons.graph.GraphProperties;
import br.edu.unifei.mestrado.commons.graph.NodeWrapper;
import br.edu.unifei.mestrado.commons.partition.AbstractPartitionIndex;

public class PartitionIndexDB extends AbstractPartitionIndex {

	private Index<Node> indexPartition = null;
	private Index<Node> indexName = null;
//	private Index<Relationship> indexEdgesPartition = null; //TODO: precisa receber um objeto valido aqui

	public PartitionIndexDB(Index<Node> indexPartition, Index<Node> indexName) {
		this.indexPartition = indexPartition;
		this.indexName = indexName;
	}

	@Override
	protected void addToSet(int setId, NodeWrapper node) {
		indexPartition.add(((NodeDB) node).getInnerNode(), GraphProperties.PARTITION, setId);
	}
	
	@Override
	protected void removeFromSet(int setId, NodeWrapper node) {
		indexPartition.remove(((NodeDB) node).getInnerNode(), GraphProperties.PARTITION, setId);
	}

//	@Override
//	protected void addToSet(int setId, RelationshipWrapper edge) {
//		indexEdgesPartition.add(((RelationshipDB) edge).getInnerRelationship(), GraphProperties.PARTITION, setId);
//	}
//	
//	@Override
//	protected void removeFromSet(int setId, RelationshipWrapper edge) {
//		indexEdgesPartition.remove(((RelationshipDB) edge).getInnerRelationship(), GraphProperties.PARTITION, setId);
//	}

	@Override
	public boolean containsKey(long idVertice) {
		Node node = indexName.get(GraphProperties.ID, idVertice).getSingle();
		if (node == null) {
			return false;
		}
		return true;
	}
	
//	@Override
//	public Integer getCutWeightBySetId(int setId) {
//		throw new RuntimeException("Precisa implementar");
//	}

	@Override
	public Iterable<NodeWrapper> queryNodes(final String key, final Integer value) {
		return new Iterable<NodeWrapper>() {

			@Override
			public Iterator<NodeWrapper> iterator() {
				return new Iterator<NodeWrapper>() {
					Iterator<Node> it = indexPartition.query(key, value).iterator();

					@Override
					public NodeWrapper next() {
						return new NodeDB(it.next());
					}

					@Override
					public boolean hasNext() {
						return it.hasNext();
					}

					@Override
					public void remove() {
					}

				};
			}
		};
	}
	
//	@Override
//	public Iterable<RelationshipWrapper> queryEdges(final String key, final Integer value) {
//		return new Iterable<RelationshipWrapper>() {
//
//			@Override
//			public Iterator<RelationshipWrapper> iterator() {
//				return new Iterator<RelationshipWrapper>() {
//					Iterator<Relationship> it = indexEdgesPartition.query(key, value).iterator();
//
//					@Override
//					public RelationshipWrapper next() {
//						return new RelationshipDB(it.next());
//					}
//
//					@Override
//					public boolean hasNext() {
//						return it.hasNext();
//					}
//
//					@Override
//					public void remove() {
//					}
//
//				};
//			}
//		};
//	}

}
